/**
 * Created by Administrator on 2017/5/20 0020.
 */
$(function(){

    $("#login").on("click",function () {
        location.href="login.html";
    })

    $("#regiter").on("click",function () {
        location.href="register.html";
    })




    $(".banner_list>.list1>li").on("mouseenter",function(){
       // alert(11)
        $(this).css("background","#fff").siblings().css("background","");
        $(this).find(">p>a").css("color","red").parents("li").siblings().find(">p>a").css("color","#fff");
        $(this).children(".subclass").css("display","block");
    })


    $(".banner_list>.list1>li").on("mouseleave",function(){
        $(this).css("background","");
        $(this).find(">p>a").css("color","");
        $(this).children(".subclass").css("display","none");
    })

    $(".subclass ul li a").on("mouseenter",function(){
        $(this).css("color","red").parents("li").siblings().find("a").css("color","");
    })

    $("#banner .wol_d span").on()


    /*nav部分*/

    $(".nav_inner ul li").on("mouseenter",function(){

        $(this).find("a").css("color","red").parent("li").siblings().find("a").css("color","");
        $(".nav_inner ul li .reguar").css("color","");

        $(this).children(".submenu").stop().fadeIn(300).css("display","block");
    })

    $(".nav_inner ul li").on("mouseleave",function(){

        $(this).children(".submenu").stop().fadeOut(300,function(){
            $(this).css("display","none");
        });//
        $(this).find("a").css("color","");
    })


    /*main_shop轮播效果*/

    var shopsize=$(".main_shop").children("div").length;

    var tmtindex=0;
    // var timerg=setInterval(function(){
    //     tmtindex++;
    //     Mainmove();
    // },1000)
    //
    // function Mainmove(){
    //     if(tmtindex>=shopsize){
    //         tmtindex=0;
    //     }
    //     if(tmtindex<0){
    //         tmtindex=shopsize-1;
    //     }
    //
    // }


    $.ajax({
        "url":"json/index.json",
        "type":"get",
        "dataType":"json",
        success:function(bann){
            var obt=bann.bannerbg;


            var tmthtml='<div class="wol">';
            tmthtml+='<div class="wol_d"><span class="current"></span></div>';
            tmthtml+='<div class="wol_d"><span></span></div>';
            tmthtml+='</div>';
            $("#banner").prepend(tmthtml);

                var tmyhtml = '<ul class="list4">';
                tmyhtml += '<li><p class="b_f">';
                tmyhtml += '<img src='+obt[0].url+'>';
                tmyhtml += '</p></li>';

                tmyhtml += '<li><p class="b_f">';
                tmyhtml += '<img src='+obt[1].url+'/>';
                tmyhtml += '</p></li>';
                tmyhtml += '</ul>';
                $("#banner").append(tmyhtml);



                    var tmpindex=0;
            /*轮播图*/
            var  timer = setInterval(function(){
                        tmpindex++;
                  move();
                    },5000)

            //轮播图的方法
            function move(){
                if(tmpindex % 2 == 1){
                    $(".list4").animate({"left" : -1920});
                    $(".wol_d span:eq(1)").addClass("current").parent().siblings().find("span").removeClass("current");
                    ;
                } else {
                    $(".list4").animate({"left" : 0});
                    $(".wol_d span:eq(0)").addClass("current").parent().siblings().find("span").removeClass("current");
                    ;
                }
            }

            //移入清除定时器
            $("#banner").on("mouseenter",function(){
                 clearInterval(timer);
            })

            $("#banner").on("mouseleave",function(){
                timer = setInterval(function(){
                    tmpindex++;
                    move();
                },5000)
            })

            }


    })


    /*小图标点击事件*/
    $("body").on("click",".wol_d span",function(){
        console.log(1111);
        $(this).addClass("current").parent().siblings().find("span").removeClass("current");

    })

    $("body").on("click",".wol_d span:eq(0)",function(){
        $(".list4").animate({"left":0});
    })

    $("body").on("click",".wol_d span:eq(1)",function(){
        $(".list4").animate({"left":-1920});
    })

    //移入小图标事件
    $("body").on("mouseenter",".wol_d span",function(){
        $(this).addClass("current");
    })

    $("body").on("mouseleave",".wol_d span",function(){
        $(this).removeClass("current");
    })

    //商品列表
    $.ajax({
        url: "json/products.json",
        type: "get",
        dataType: "json",
        success: function (res) {

            var obj=res.products;

            for(var j=0;j<6;j++){


            for(var i=obj.length-1; i>=0; i--){

                var tmphtml = '<div class="item">';
                tmphtml += '<span class="good_local"><img src="img/goods-local.png"/></span>';
                tmphtml += '<a class="item_bo"><img class="bo" src=' + obj[i].url + '></a>';
                tmphtml += '<p class="shop_name">' + obj[i].proname + '</p>';
                tmphtml += '<p class="bnef">' + obj[i].bnef + '</p>';

                tmphtml += '<div class="price">';
                tmphtml += '<span class="price_r">' + obj[i].price + '</span>';
                tmphtml += '<div class="price_del">';
                tmphtml += '<span>￥245.00</span>';
                tmphtml += '<span>立省￥245.00</span>';
                tmphtml += '</div>';
                tmphtml += '<input type="button" value="加入购物车" class="add"/>';
                tmphtml += '</div>';
                tmphtml += '</div>';

                $(".products").html(tmphtml + $(".products").html());


            }
        }

            $("body").on("click",".item",function(){
               var index = $(this).index();
                index=index%obj.length;
                console.log(obj[index].proid);
                location.href="Commodity details.html?"+obj[index].proid;
            })
        }

    })

    $(".cart_buy").on("click",function(){
        location.href="shoppingcart.html";
    })


    /*点击购物车图标显示*/
    $(".shop_a:eq(0)").click(function(){

        $(".shopshow").animate({"right":"40px"},500);
    })

    /*点击x图标隐藏*/
    $(".shopshow .stitle .p2").click(function(){

        $(".shopshow").animate({"right":"-300px"},500);
    })

    //返回顶部
    $(".shop_a:eq(5)").on("click",function(){

            var timer=setInterval(function(){
            //1,current
            var current=document.body.scrollTop||document.documentElement.scrollTop;

            //2.speed
            var speed=(0-current)/8;
            speed=Math.floor(speed);

            //3.临界值
            if(current==0){
                clearInterval(timer);
                return;
            }

            //运动
            document.documentElement.scrollTop=document.body.scrollTop=current+speed;

        },20)
    })

    /*添加商品到购物车*/
    var btn=$(".products .item .add");
    var shop=$(".shopshow");

    var fleg=false;

    //var offset = $("#cart").offset();


    $.ajax({
        url: "json/products.json",
        type: "get",
        dataType: "json",
        success:function(reg){
            var obc=reg.products;

            var iner=0;
            //var arr = [];
            $("body").on("click",".add",function(event){
               iner = $(this).parents(".item").index();
                iner = iner%obc.length;
                event.stopPropagation();

                var addcar = $(this);
                var img = addcar.parent().parent().find('.bo').attr('src');



                //信息存入cookie
                var flyer = $("<img class='u-flyer'/>"); //创建img节点, 给一个class=u-flyer
                flyer.attr("src",img); //给img一张图

                //调用fly, 让图片飞
                flyer.fly({

                    //起始位置
                    start: {
                        left: event.clientX,
                        top: event.clientY,
                        width: 90,
                        height: 90
                    },
                    //结束位置
                    end: {
                        left: $("#cart").offset().left- $(window).scrollLeft(),
                        top: $("#cart").offset().top- $(window).scrollTop()+80,

                        width: 0,
                        height:0

                    },
                    //结束后
                    onEnd: function(){
                        flyer.fadeOut();
                        console.log("加入购物车成功!");
                    }

                });


                var shopcart='<div class="cart_bot">';
                shopcart+='<div class="cart_po">极速仓</div>';
                shopcart+='<ul>';
                shopcart+='<li>';
                shopcart+='<div class="pro_bg"><a href="javascript:void(0);"><img src="'+obc[iner].url+'"></a></div>';
                shopcart+='<div class="produ_r">';
                shopcart+='<div class="item_tit">'+obc[iner].proname+'</div>';
                shopcart+='<div class="add_po">';
                shopcart+='<a class="reg_t"><i class="reg"></i></a>';
                shopcart+='<span class="pro_num">'+obc[iner].cartnumber+'</span>';
                shopcart+='<a class="reg_t"><i class="addg"></i></a>';
                shopcart+='</div>';
                shopcart+='<div class="price_pro">';
                shopcart+='<span>'+obc[iner].price+'</span><span class="del"></span>';
                shopcart+='</div>';
                shopcart+='</div>';
                shopcart+='</li>';
                shopcart+='</ul>';
                shopcart+='</div>';

                $(".cart_content").append(shopcart);



                var arr = $.cookie("cart")?JSON.parse($.cookie("cart")):[];
                    /*商品存入cookie*/


                var isexs = false
                for (var i =0 ; i<arr.length;i++ ) {

                    if(arr[i].proid == obc[iner].proid){
                        arr[i].cartnumber++;
                        isexs  =true

                    }
                }

                if(isexs ==false ){
                    obc[iner].cartnumber = 1;

                    //checkbox
                    arr.push(obc[iner]);

                }

                        $.cookie("cart",JSON.stringify(arr),{expires : 30, path : "/"});



            });

        }

    })

    //减号
    $("#cart").on("click",".reg",function(){
        $(this).next().html(parseInt($(this).next().html())-1);
    })

    //加号
    $("#cart").on("click",".addg",function(){
        $(this).prev().html(parseInt($(this).prev().html())+1);

    })

    //鼠标移入删除图标变色
    $("#cart").on("mouseenter",".del",function(){
        $(this).css({"background-position-x":-176,"background-position-y":0})
    })

    $("#cart").on("mouseleave",".del",function(){
        $(this).css({"background-position-x":-146,"background-position-y":0})

    })


    $("#cart").on("click",".del",function(){
        var bool=confirm("确定要删除该商品吗?");
        if(bool){
            $(this).closest(".cart_bot").remove();
        }
    })



})









